package com.earstep.threadpool;


public class TimerTask {
	private TimerRunnable client;
	private long timeDelay;// 延迟时间
	private long timeNext;// 下一次执行该任务的时间
	private boolean repeat;// 是否重复执行
	private boolean suspended;// 是否暂停
	private long timeSpanceSuspended = 0;// 暂停间隔时间
	private boolean deletePending;// 是否正在删除

	public TimerTask(TimerRunnable client) {
		this.client = client;
		this.timeDelay = client.getDelay();
		this.repeat = client.isRepeat();
		timeNext = System.currentTimeMillis();
		if (!repeat)
			timeNext = System.currentTimeMillis() + timeDelay;
		deletePending = false;
	}

	public void suspended() {// 暂停任务
		timeSpanceSuspended = timeNext - System.currentTimeMillis();
		suspended = true;
	}

	public void start() {// 开始任务
		suspended = false;
		timeNext = System.currentTimeMillis() + timeSpanceSuspended;
	}

	public void test() {// 判断执行时间是否到来 到来则执行
		if (System.currentTimeMillis() >= timeNext) {
			ThreadPool.queueItem(client);
			timeNext = System.currentTimeMillis() + timeDelay;// 该任务下次执行时间
			deletePending = !repeat;
			client.startEvent();
		}
	}

	public long getTimeNext() {
		return timeNext;
	}

	public boolean isDeletePending() {
		return deletePending;
	}

	public boolean isSuspended() {
		return suspended;
	}

	public void setDeletePending(boolean deletePending) {
		this.deletePending = deletePending;
	}

	public void setRepeat(boolean repeat) {
		this.repeat = repeat;
	}

	public TimerRunnable getClient() {
		return client;
	}

	public void setSuspended(boolean suspended) {
		this.suspended = suspended;
	}
}